package com.ruoyi.project.dict.approve.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.approve.domain.CiSentenceApprove;
import com.ruoyi.project.dict.approve.service.ICiSentenceApproveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 造句审核Controller
 *
 * @author shixiangwen
 * @date 2020-10-20
 */
@Controller
@RequestMapping("/dict/approve")
public class CiSentenceApproveController extends BaseController {
    private String prefix = "dict/approve";

    @Autowired
    private ICiSentenceApproveService ciSentenceApproveService;

    @RequiresPermissions("dict:approve:view")
    @GetMapping()
    public String approve() {
        return prefix + "/approve";
    }

    /**
     * 查询造句审核列表
     */
    @RequiresPermissions("dict:approve:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CiSentenceApprove ciSentenceApprove) {
        startPage();
        List<CiSentenceApprove> list = ciSentenceApproveService.selectCiSentenceApproveList(ciSentenceApprove);
        return getDataTable(list);
    }

    /**
     * 导出造句审核列表
     */
    @RequiresPermissions("dict:approve:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CiSentenceApprove ciSentenceApprove) {
        List<CiSentenceApprove> list = ciSentenceApproveService.selectCiSentenceApproveList(ciSentenceApprove);
        ExcelUtil<CiSentenceApprove> util = new ExcelUtil<CiSentenceApprove>(CiSentenceApprove.class);
        return util.exportExcel(list, "approve");
    }

    /**
     * 新增造句审核
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存造句审核
     */
    @RequiresPermissions("dict:approve:add")
    @Log(title = "造句审核", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CiSentenceApprove ciSentenceApprove) {
        return toAjax(ciSentenceApproveService.insertCiSentenceApprove(ciSentenceApprove));
    }

    /**
     * 修改造句审核
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CiSentenceApprove ciSentenceApprove = ciSentenceApproveService.selectCiSentenceApproveById(id);
        mmap.put("ciSentenceApprove", ciSentenceApprove);
        return prefix + "/edit";
    }

    /**
     * 修改保存造句审核
     */
    @RequiresPermissions("dict:approve:edit")
    @Log(title = "造句审核", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CiSentenceApprove ciSentenceApprove) {
        return toAjax(ciSentenceApproveService.updateCiSentenceApprove(ciSentenceApprove));
    }

    @RequiresPermissions("dict:approve:edit")
    @Log(title = "造句审核", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(Long id, Integer status) {
        return toAjax(ciSentenceApproveService.approve(id, status));
    }


    @RequiresPermissions("dict:approve:edit")
    @Log(title = "造句审核", businessType = BusinessType.UPDATE)
    @PostMapping("/batchApprove")
    @ResponseBody
    public AjaxResult batchApprove(String ids, Integer status) {

        String[] strIds = Convert.toStrArray(ids);
        int count = 0;
        for (String strId : strIds) {
            Long id = Long.parseLong(strId);
            ciSentenceApproveService.approve(id, status);
            count += 1;
        }

        return toAjax(count > 0);
    }


    /**
     * 删除造句审核
     */
    @RequiresPermissions("dict:approve:remove")
    @Log(title = "造句审核", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ciSentenceApproveService.deleteCiSentenceApproveByIds(ids));
    }
}
